package cn.iocoder.yudao.module.worker.controller.app.clerkapply;

import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.worker.controller.admin.category.vo.CategoryRespVO;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerklevel.ClerkLevelDO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.worker.controller.app.clerkapply.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerkapply.ClerkApplyDO;
import cn.iocoder.yudao.module.worker.service.clerkapply.ClerkApplyService;

@Tag(name = "用户 APP - 店员申请")
@RestController
@RequestMapping("/worker/clerk-apply")
@Validated
public class AppClerkApplyController {

    @Resource
    private ClerkApplyService clerkApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建店员申请")
    @PreAuthenticated
    public CommonResult<Long> createClerkApply(@Valid @RequestBody AppClerkApplySaveReqVO createReqVO) {
        createReqVO.setUserId(getLoginUserId());
        return success(clerkApplyService.createClerkApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店员申请")
    @PreAuthenticated
    public CommonResult<Boolean> updateClerkApply(@Valid @RequestBody AppClerkApplySaveReqVO updateReqVO) {
        updateReqVO.setUserId(getLoginUserId());
        clerkApplyService.updateClerkApply(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateOnlineStatus")
    @Operation(summary = "更新店员在线状态")
    @PreAuthenticated
    public CommonResult<Boolean> updateOnlineStatus(@Valid @RequestBody AppClerkApplySaveReqVO updateReqVO) {
        updateReqVO.setUserId(getLoginUserId());
        clerkApplyService.updateOnlineStatus(updateReqVO);
        return success(true);
    }

    @PutMapping("/save")
    @Operation(summary = "更新技能管理")
    @PreAuthenticated
    public CommonResult<Boolean> updateGoodsList(@Valid @RequestBody AppClerkApplySaveReqVO updateReqVO) {
        updateReqVO.setUserId(getLoginUserId());
        clerkApplyService.updateGoodsList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店员申请")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteClerkApply(@RequestParam("id") Long id) {
        clerkApplyService.deleteClerkApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<AppClerkApplyRespVO> getClerkApply(@RequestParam("id") Long id) {
        ClerkApplyDO clerkApply = clerkApplyService.getClerkApply(getLoginUserId(), id);
        return success(BeanUtils.toBean(clerkApply, AppClerkApplyRespVO.class));
    }

    @GetMapping("/getClerkLevel")
    @Operation(summary = "获得店员等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult getClerkLevel(@RequestParam("id") Long id) {
        ClerkLevelDO levelDO = clerkApplyService.getClerkLevel(id);
        return success(levelDO.getName());
    }

    @GetMapping("/list")
    @Operation(summary = "获得技能配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<CategoryRespVO>> getGoodsList(@RequestParam("id") Long id) {
        List<CategoryRespVO> list = clerkApplyService.getAppGoodsList(id);
        return success(BeanUtils.toBean(list, CategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员申请分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppClerkApplyRespVO>> getClerkApplyPage(@Valid AppClerkApplyPageReqVO pageReqVO) {
        pageReqVO.setUserId(getLoginUserId());
        PageResult<ClerkApplyDO> pageResult = clerkApplyService.getClerkApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppClerkApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店员申请 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClerkApplyExcel(@Valid AppClerkApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClerkApplyDO> list = clerkApplyService.getClerkApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店员申请.xls", "数据", AppClerkApplyRespVO.class,
                        BeanUtils.toBean(list, AppClerkApplyRespVO.class));
    }

}